package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author mqx
 * @date 2020-8-13 15:15:44
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String paymentType) {
        // 细节处理：paymentInfo 表中： 订单Id 和 支付方式 这个两个作为一个标准，不能重复。
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderInfo.getId());
        paymentInfoQueryWrapper.eq("payment_type",paymentType);
        Integer count = paymentInfoMapper.selectCount(paymentInfoQueryWrapper);
        if (count>0){
            return;
        }
        // 创建一个paymentInfo 对象
        PaymentInfo paymentInfo = new PaymentInfo();
        // 给paymentInfo 赋值
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setCreateTime(new Date());

        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String name) {
        // select * from payment_info where out_trade_no=outTradeNo and payment_type = name
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type",name);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        return paymentInfo;
    }

    @Override
    public void paySuccess(String outTradeNo, String name, Map<String, String> paramMap) {
        // 获取paymentInfo 对象
        PaymentInfo paymentInfoQuery = getPaymentInfo(outTradeNo, name);
        // 判断查询到的数据支付状态
        if (paymentInfoQuery.getPaymentStatus().equals(PaymentStatus.PAID.name()) ||
        paymentInfoQuery.getPaymentStatus().equals(PaymentStatus.ClOSED.name())){
            // 此时需要return
            return;
        }
        // 设置更新的内容
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramMap.toString());
        paymentInfo.setTradeNo(paramMap.get("trade_no"));
        // 调用更新方法。
        updatePaymentInfo(outTradeNo,paymentInfo);
        // 发送一个消息：告诉订单，并更改订单的状态！异步操作！
        // 发送的数据应该是orderId ，根据这个orderId 更新订单的状态。
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,
                paymentInfoQuery.getOrderId());
    }


    // 单独提出来做一个可以重用的方法
    public void updatePaymentInfo(String outTradeNo,PaymentInfo paymentInfo){
        // 设置更新的条件
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
    }

    @Override
    public void closePayment(Long orderId) {
        // 判断交易记录表中是否有数据， 在点击支付宝支付的时候，才会生成数据。
        // select * from payment_info where order_id=orderId
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderId);
        Integer count = paymentInfoMapper.selectCount(paymentInfoQueryWrapper);
        if (count==0){
            // 停止
            return;
        }
        // 关闭交易记录
        // update payment_info set payment_status=PaymentStatus.ClOSED.name() where order_id = orderId
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);

    }
}
